package algorithm_study.leecode;

public class IslandPerimeter {
    private int roundLen=0;
    private boolean[][]flag;

    public static void main(String[] args) {
        int[][] grids = new int[1][2];
        grids[0][0]=0;
        grids[0][1]=1;
        new IslandPerimeter().islandPerimeter(grids);
    }
    public  int islandPerimeter(int[][] grid) {
        flag=new boolean[grid.length][grid.length];
        for(int i=0;i<grid.length;++i){
            for(int j=0;j<grid.length;++j){
                if(grid[i][j]==1){
                    dfs(i,j,grid);
                    return roundLen;
                }
            }
        }
        return roundLen;
    }
    public void dfs(int x,int y,int[][] grid){
        flag[x][y]=true;
        //上
        if(x>0&&grid[x-1][y]==1){
            if(!flag[x-1][y])
                dfs(x-1,y,grid);
        }else{
            ++roundLen;
        }
        //下
        if(x<grid.length-1&&grid[x+1][y]==1){
            if(!flag[x+1][y])
                dfs(x+1,y,grid);
        }else{
            ++roundLen;
        }
        //左
        if(y>0&&grid[x][y-1]==1){
            if(!flag[x][y-1])
                dfs(x,y-1,grid);
        }else{
            ++roundLen;
        }
        //右
        if(y<grid.length-1&&grid[x][y+1]==1){
            if(!flag[x][y+1])
                dfs(x,y+1,grid);
        }else{
            ++roundLen;
        }
    }
}
